package com.zoe.wan.android.example.activity.login

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.view.View
import com.blankj.utilcode.util.SPUtils
import com.youth.banner.util.LogUtils
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.databinding.ActivityLoginBinding
import com.zoe.wan.base.BaseActivity
import com.zoe.wan.android.example.BR
import com.zoe.wan.android.example.Constants.SP_USER_NAME
import com.zoe.wan.android.example.activity.tab.TabActivity

class LoginActivity :BaseActivity<ActivityLoginBinding,LoginViewModel>() {

    companion object{
        // 0代表登录
        const val Intent_type_login = 0
        const val Intent_type_name = "Intent_type_name"
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun getViewModelId(): Int {
        return BR.loginVm
    }

    override fun initViewData() {

        val type = intent.getIntExtra(Intent_type_name, -1)
        if (type == Intent_type_login){
            binding?.inputUserPwdAgain?.visibility = View.GONE
            binding?.register?.visibility = View.VISIBLE
            binding?.loginOrRegist?.text = "确认登录"
        }else{
            binding?.inputUserPwdAgain?.visibility = View.VISIBLE
            binding?.register?.visibility = View.GONE
            binding?.loginOrRegist?.text = "确定注册"
        }

        binding?.register?.setOnClickListener{
            val intent = Intent(this,LoginActivity::class.java)
            startActivity(intent)
        }

        binding?.loginOrRegist?.setOnClickListener{
            if (type == Intent_type_login){
                login()
            }else{
                register()
            }
        }
        binding?.inputUserName?.setText("woaichikaorou")
    }


    fun login(){

        viewModel?.login{name ->
            SPUtils.getInstance().put(SP_USER_NAME,name)
            LogUtils.d("SP存值" + SPUtils.getInstance().getString(SP_USER_NAME,""))
            finish()
            startActivity(Intent(this,TabActivity::class.java))
        }
    }

    fun  register(){
        viewModel?.register {
            finish()
            startActivity(Intent(this,LoginActivity::class.java))

        }
    }

    fun startIntent(clas: Class<*>){
        startActivity(Intent(this,clas))
    }
}